package com.h3bpm.kumhosunny.crm.controller;

import OThinker.Common.DateTimeUtil;
import OThinker.H3.Controller.ControllerBase;
import com.h3bpm.base.res.ResBody;
import com.h3bpm.kumhosunny.common.utils.SqlUtil;
import data.DataTable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

/**
 * @authormi
 * @date2019/9/25
 */
@Controller
@RequestMapping("/Portal/CRM/TravelNum")
public class TravelProjectNumberController extends ControllerBase {

    private static final Logger LOGGER = LoggerFactory.getLogger(TravelProjectNumberController.class);
    @Override
    public String getFunctionCode() {
        return null;
    }



    /****
     * 功能描述: 获取项目编号 每月更新。取 年月 + 三位递增数
     *
     * @Param: []
     * @Return: com.h3bpm.base.res.ResBody
     * @Author: zhengyj
     * @Date: 2019/9/20 13:56
     */
    @RequestMapping(value = "/getSystemProjectNumber", method = RequestMethod.GET,produces = "text/html;charset=UTF-8")
    @ResponseBody
    public ResBody getSystemProjectNumber(){
        LOGGER.warn("==========获取项目编号 每月更新===========");
        String returnProjectNumber = "";
        Date date = new Date();
        String dateStr = DateTimeUtil.getYear(date) + "" + String.format("%02d",DateTimeUtil.getMonth(date));
        String querySql = " SELECT orderNumber from I_travelAppl WHERE orderNumber LIKE '" + dateStr + "%' ORDER BY orderNumber DESC LIMIT 1 ";
        try {
            DataTable dataTable = SqlUtil.doQuery(querySql);
            if (dataTable.getRows()!= null && dataTable.getRows().size() > 0) {
                String projectNumber = dataTable.getRows().get(0).getString(0);
                int num = Integer.valueOf(projectNumber.split(dateStr)[1]);
                returnProjectNumber = dateStr + String.format("%03d",num + 1);
            } else {
                returnProjectNumber = dateStr + "001";
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        LOGGER.info("=======生成的项目编号======" + returnProjectNumber);
        return ResBody.buildSuccessResBody(returnProjectNumber);
    }


}
